#!/bin/sh
#
# rm-all -- remove all docker labs
#
# Copyright (C) 2016-2020 Wu Zhangjin <lzufalcon@163.com>
#

export RELEASE=0

uname | grep -q MINGW && PWD_OPT="-W"
TOP_DIR="$(cd "$(dirname "$0")"/../../ && pwd $PWD_OPT)"
. "$TOP_DIR/tools/docker/config" $* >/dev/null

id_names=`docker ps -f status=running -f status=exited --format='{{.ID}},{{.Names}}' | sed '1!G;h;$!d'`

for id_name in $id_names
do
    id=$(echo $id_name | cut -d ',' -f1)
    name=$(echo $id_name | cut -d ',' -f2)

    if [ "$name" = "$WPROXY_NAME" -o "$name" = "$TPROXY_NAME" ]; then
        echo "Removing $name ..."
        docker rm -f $name
        continue
    fi


    lab=`echo $name | sed -e "s%-[0-9]*$%%g"`
    echo $LABS | tr ' ' '\n' | grep -q ^$lab$
    [ $? -ne 0 ] && continue

    echo "Removing $name ..."

    ls "${LAB_OUTPUT}/$name*/" >/dev/null 2>&1
    if [ $? -eq 0 ]; then
        "${LAB_OUTPUT}/$name*/tools/docker/rm" $lab
    elif [ -d "${TOP_DIR}/configs/$lab" ]; then
        "$DOCKER_RM_CMD" $lab
    else
        docker rm -f $id
    fi

done

$DOCKER_RELEASE_CMD all >/dev/null 2>&1
